/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package barbershop;

import java.util.Random;

/**
 *
 * @author Гамзатов
 */

public class Barber extends Thread {
    public int bCust;
    final Random randomGenerator = new Random();

    @Override
    public void run() {
    while (variables.alldone < variables.customerall) {
        try {
            variables.cutReady.acquire();
            variables.mutex2.acquire();
    	    bCust = variables.enqueue.poll();
    	    variables.mutex2.release();
    	    variables.coord.acquire();
    	    
            //врея стрижки.
            int t = randomGenerator.nextInt(5000);

            System.out.println("The barber is cutting hair " + bCust + " " + t);
            try {
                sleep(t);
            } catch (InterruptedException ex) {
            }
            variables.coord.release();
            if (bCust < variables.customerall) {
                variables.finished[bCust].release();
        	}
        	variables.leaveBChair.acquire();
            variables.barberChair.release();
        } catch (InterruptedException e) {
        }
    	}
    }
    
}